//
//  YHCycleImage.m
//  06_ScrollView(CycleImages)
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "YHCycleImage.h"
// 延展
@interface YHCycleImage () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagesPage;
@end




@implementation YHCycleImage
#pragma mark - scrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 改变pageControl的对应下标
    _imagesPage.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page - 1;
    if (page == 0) {
        // 改变偏移量
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_array.count - 2), 0);
         _imagesPage.currentPage = _array.count - 1 - 2;
    }
    if (page == _array.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
    }
}
// 1. 初始化
#pragma mark - initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor cyanColor];
        // 子控件初始化时 frame不要与初始化的frame一样 可以写self.bounds 或者 CGRectMake(0, 0, frame.size.width, frame.size.height)
        [self createScrollView];
        [self createImagePage:frame];
        }
    return self;
}

- (void)createScrollView {
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.backgroundColor = [UIColor redColor];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.delegate = self; // 指定代理人 让小点动起来
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_cycleScrollView];
}

// pageControl 后期需要添加page的位置改变 这里是默认位置
- (void)createImagePage:(CGRect)frame {
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _imagesPage.numberOfPages = 5;
    _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 10);
    [self addSubview:_imagesPage];
    [_imagesPage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)imagePageChange:(UIPageControl *)pageControl {
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}

#pragma mark - imageArr
- (void)createImagesWithArray:(NSArray *)array {
    // 根据传过来的数组进行判断生成新的数组
    NSArray *imagesArray = [self handleArray:array];
    // 做判断时需要用到属性
    self.array = imagesArray;
    // 使用数组创建imageView
    [self createAllImageViewsWithImageArray: imagesArray];
    // 根据图片数组 设置对应视图
       _imagesPage.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
    // 因为添加了最后一张图 改变偏移量 从第一张图显示
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}

- (NSArray *)handleArray:(NSArray *)array {
    id firstObject = [array firstObject]; // 判断类型 因为数组里的类型不确定
    NSMutableArray *imagesArray = [NSMutableArray array]; // 最终图片数组
    if ([firstObject isKindOfClass:[NSString class]]) { // 判断数组中元素的类型
        // 将字符串数组转化为 image数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        // 直接使用数组创建imageView
        [imagesArray setArray:array];
    }
    // 获取第一张图 获取最后一张图 将最后一张图放在数组第一位 将第一张图方在数组最后一位
    UIImage *firstImage = [imagesArray firstObject];
    UIImage *lastImage = [imagesArray lastObject];
    [imagesArray insertObject:lastImage atIndex:0];
    [imagesArray addObject:firstImage];

    return [imagesArray copy];
}

- (void)createAllImageViewsWithImageArray:(NSArray *)imagesArray {
    for (NSInteger i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}






@end
